package com.cortex.pineapple.fetch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.cortex.pineapple.database.VisitLink;
import com.cortex.pineapple.utilities.MessagesLog;
import com.cortext.pineapple.link.Link;
import com.cortext.pineapple.link.NewsLink;
import com.cortext.pineapple.link.NewsLink;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.util.Log;



public class FetchVisitLink {

	private OnFetchVisitLinkChangedListener onFetchWebDescriptionChangedListener;

	public void startFetching(VisitLink visitLink){
		new StartWebThread().execute(visitLink);
	}
	
	public void setOnFetchWebDescriptionChangedListener(
			OnFetchVisitLinkChangedListener onFetchWebDescriptionChangedListener) {
		this.onFetchWebDescriptionChangedListener = onFetchWebDescriptionChangedListener;
	}
	private class StartWebThread extends AsyncTask<VisitLink, Void, VisitLink>{

		private Document doc;

		@Override
		protected VisitLink doInBackground(VisitLink... params) {
			VisitLink visitLink = params[0];
			String title = "";
			String description = "";
			try {
				String url = visitLink.getUrl();
				if(!url.contains("http://")){
					url = "http://" + visitLink.getUrl();
				}
				connectToUrl(url);
				title = doc.title();
				visitLink.setTitle(title);
				
				description = doc.select("meta[name=description]").first().attr("content");
				visitLink.setDescription(description);
				
			} catch (IOException e) {
				Log.d(MessagesLog.APPLICATION_EXCEPTION,"throw exception when connect to url message:" + e.getMessage());
			} catch (Exception e) {
				Log.d(MessagesLog.APPLICATION_EXCEPTION,"throw general exception message:" + e.getMessage());
			}
			return visitLink;
		}
		@Override
		protected void onPreExecute() {
			if(onFetchWebDescriptionChangedListener != null){
				onFetchWebDescriptionChangedListener.visitLinkTimeLoadStarted();
			}
		}

		@Override
		protected void onPostExecute(VisitLink result) {
			if(onFetchWebDescriptionChangedListener != null){
				onFetchWebDescriptionChangedListener.visitLinkTimeLoadFinished(result);
			}
		}
		@Override
		protected void onCancelled() {
			if(onFetchWebDescriptionChangedListener != null){
				onFetchWebDescriptionChangedListener.visitLinkTimeLoadCancelled();
			}
		}
		private void connectToUrl(String url) throws IOException{
			doc = Jsoup.connect(url).get();
		}

	}

	public interface OnFetchVisitLinkChangedListener{
		void visitLinkTimeLoadStarted();
		void visitLinkTimeLoadFinished(VisitLink link);
		void visitLinkTimeLoadCancelled();
	}

}
